我有一个表单,其中有一个文本框,它是div标记的一部分。我希望它根据phpGET请求中文本框的内容将div标记的内容更改为另一个页面。
例如,如果在文本框中键入hell,它将加载goto.php?location=地狱般的潜水。
到目前为止,这是我在下面的代码,由于这是我第一次使用jquery,它比胡佛吸尘器更糟糕。
<div id="city"></div>
<form name="goto" action="/">
<input type="text" id="city">
<input type="submit" name="submit" class="button" id="submit_btn" value="New City" />
</form>
<script>
/* attach a submit handler to the form */
$("#goto").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/* get some values from elements on the page: */
var $form = $( this ),
city = $form.find( 'input[name="city"]' ).val(),
/* Send the data using post and put the results in a div */
$('currentwxdiv').load('http://www.jquerynoob.bomb/hell.php?location=' + city);
});
</script>
目前,更新div的内容会在浏览器中重新加载整个页面,然后添加?submit=新建+城市
UPDATE这就是我现在所拥有的,它仍然在用附加的来刷新整个页面?submit=新建+城市
<script>
/* attach a submit handler to the form */
$$('form[name="changewx"]').submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/* get some values from elements on the page: */
var $form = $( this ),
city = $('#city').val()
/* Send the data using post and put the results in a div */
$('#currentwxdiv').load('http://api.mesodiscussion.com/?location=' + city);
});
</script>
您已经创建了这样的表单:
<form name="goto" action="/">
但是选择器只匹配id为goto
的元素。所以应该是:
$('form[name="goto"]').submit(function(event) {
你也犯了一个错误:
$form.find( 'input[name="city"]' ).val()
应为:
$('#city').val()
另一件事:
$('currentwxdiv') // this matches a tag called currentwxdiv
应为:
$('.currentwxdiv') // for element with class currentwxdiv, or $('#currentwxdiv') to match based on id
应该在函数末尾执行return false;
,而不是执行e.stopPropagation();
。