[英]Javascript: Checked radio button
In one page I have two form for search: 在一页中,有两种搜索形式:
<form class="search-panel" method="post" action="" onsubmit="return searchRedirect(this)">
<input type="test" name="searchFor" />
<div class="additional-search-button"></div>
<div id="additional-search-box">
<div class="as-cont">
Books<input type="radio" name="category" value="Books" checked="1" /><br/>
School<input type="radio" name="category" value="School" /><br/>
Music<input type="radio" name="category" value="Music" />
</div>
</div>
<input type="submit" name="search" />
</form>
<form class="search-panel" method="post" action="" onsubmit="return searchRedirect(this)">
<input type="test" name="searchFor" />
Games<input type="radio" name="category" value="Games" checked="1" />
<input type="submit" name="search" />
</form>
My problem is that if I click search for Games in searchRedirect always alert Books, School or Music if they checked: 我的问题是,如果我单击“搜索重定向”中的“搜索游戏”,则如果选中了“书籍”,“学校”或“音乐”,则会始终发出警报:
That is my javascript function: 那就是我的javascript函数:
function searchRedirect(form) {
alert($(form['category']+':checked').val());
if($(form['category']+':checked').val() == 'Форум')
window.location.href = '/forum/search.php?keywords='+form['searchFor'].value;
else {
window.location.href = '/Search/'+$(form['category']+':checked').val()+'/'+form['searchFor'].value;
}
return false;
}
I need if I click to search games - search only for games, if click to search books,school or music - searching only for them. 如果我单击以搜索游戏,则仅需要搜索游戏;如果单击以搜索书籍,学校或音乐,则需要仅搜索它们。 But now search form for games always use first form checked buttons.
但是现在,游戏的搜索表单始终使用第一个表单选中的按钮。
form['category']
will give you a NodeList
of all form elements whose name is category
, thus you can't use it as a selector by concatenating it with :checked
. form['category']
将为您提供所有名称为category
表单元素的NodeList
,因此您不能通过将它与:checked
串联来用作选择器。 It'd be the equivalent of $("[object NodeList]:checked")
. 相当于
$("[object NodeList]:checked")
。
Since you are already using jQuery, you can use the following instead: 由于您已经在使用jQuery,因此可以改用以下内容:
alert($(form).find("input[name='category']:checked").val());
That is, within the context of form
, find every input
element with name category
that is checked
, and get its value
. 也就是说,在
form
的上下文中,找到每个具有checked
名称category
input
元素,并获取其value
。
you can't real do form["category"] + ":checked"
, instead use jQuery's filter method and than use 'this' to reference the form. 您不能真正使用
form["category"] + ":checked"
,而是使用jQuery的filter方法,而不使用'this'来引用表单。 Also, you can/should use jQuery .submit() to catch the event instead of an inline handler and just for clearer code (and also for better performance) put the $(form['category']+':checked')
into a variable so jquery won't need to search for the element again and again. 另外,您可以/应该使用jQuery .submit()代替内联处理程序来捕获事件,并且只是为了更清晰的代码(也为了提高性能),将
$(form['category']+':checked')
放入一个变量,因此jquery不需要一次又一次地搜索元素。 This is a sample: 这是一个示例:
$('form').submit(function(e){
var checked = $(this['category']).filter(':checked');
var value = checked.val();
alert(value);
})
This is a fiddle so you can fiddle (the e passed to the submit callback is the event object and e.preventDefault is like returning false so the form won't submit): http://jsfiddle.net/SJ7Vd/ 这是一个小提琴,因此您可以进行小提琴(传递给Submit回调的e是事件对象,而e.preventDefault就像返回false一样,因此该表单将不会提交): http : //jsfiddle.net/SJ7Vd/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.