简体   繁体   English

jQuery单选按钮功能无法正常工作

[英]jQuery Radio Button function not working properly

I have a form with two radio buttons and a submit button which leads to a specific form based upon the user's selection. 我有一个带有两个单选按钮和一个提交按钮的表单,该表单会根据用户的选择生成一个特定的表单。 I wanted to use jQuery to change between the two buttons but have gotten myself a bit lost. 我想使用jQuery在两个按钮之间切换,但让自己迷失了方向。

Here is my javascript from another file in the proj: 这是我来自项目文件中另一个文件的javascript:

function goTo()
{

var yesButton = $('#yesRad');
var noButton = $('#noRad');

if (yesButton[0].checked) 
{
submitForm('yesForm') && noButton.Checked==false;


}
else  (noButton[1].checked)
{
submitForm('noForm') && yesButton.Checked==false;
}

Inside the jsp I have the following code: 在jsp中,我有以下代码:

<form:form action="interested" commandName="user" name="yesForm" id="yesForm">
    <input type="hidden"  name="state" value="<c:out value="${requestScope.state}"/>" />
    <input type="hidden"  id="address" name="address" value="${user.address}" />
    <input type="hidden"  name="mode" value="1" />
    <input type="radio"  name ="radio"id="yesRad" value="yesForm" checked="checked" />Yes<br>
</form:form>

<form:form action="notinterested" commandName="user" name="noForm" id="noForm">

    <input type="hidden"  name="state" value="<c:out value="${requestScope.state}"/>" />
    <input type="hidden"  id="address" name="address" value="${user.address}" />
    <input type="hidden"  name="mode" value="1" />
    <input type="radio" name="radio" id="noRad"  value="noForm" />No<br>
</form:form>

Submit 提交

<script>
    $("#yesRad").change(function(){
        var $input = $("#yesRad");
        var $inputb = $("#noRad");

        if($inputb.is(':checked'))
            $("#yesRad").prop("checked", false);
        else  if($input.is(':checked'))
            $("#yesRad").prop("checked",true) && $("#noRad").prop("checked",false);

});
</script>

I have gotten some functionality out of my jQuery but it's definitely far from correct.. I hope I was clear and thorough in my question. 我已经从jQuery中获得了一些功能,但是绝对不正确。.我希望我的问题清楚而透彻。 Thanks in advance!! 提前致谢!!

To begin with, don't use prop, use attr. 首先,不要使用prop,请使用attr。 prop is slower. 支柱较慢。 You've defined variables so let's not look them up again. 您已经定义了变量,所以我们不再查找它们。 In your if/else statement just use the variables. 在if / else语句中,只需使用变量即可。 I'm not entirely sure what you're trying to do with the &&. 我不确定您要对&&做什么。 I suspect you're trying to set the value of the two inputs. 我怀疑您正在尝试设置两个输入的值。 If so, they should be separate statements. 如果是这样,它们应该是单独的语句。 If inputb is checked there is no reason to set it to checked, so we can remove that piece. 如果inputb被选中,则没有理由将其设置为checked,因此我们可以删除该部分。

You probably want this change to fire on both inputs. 您可能希望此更改在两个输入上触发。

$("#yesRad, #noRad").change(function(){
    var $input = $("#yesRad");
    var $inputb = $("#noRad");
    if($inputb.is(':checked')){
        $input.attr("checked", false);
    } else if($input.is(':checked')){
        $inputb.attr("checked",false);
    }
});

Solved: Using javascript and taking the radio buttons out of the separate form elements. 解决:使用javascript并将单选按钮移出单独的表单元素。 First let's take a look at the JSP form elements involved: 首先,让我们看一下所涉及的JSP表单元素:

<form:form action="interested" commandName="user" name="yesForm" id="yesForm">
    <input type="hidden"  name="state" value="<c:out value="${requestScope.state}"/>" />
    <input type="hidden"  id="address" name="address" value="${user.address}" />

</form:form>

<form:form action="notinterested" commandName="user" name="noForm" id="noForm">
    <input type="hidden"  name="state" value="<c:out value="${requestScope.state}"/>" />
    <input type="hidden"  id="address" name="address" value="${user.address}" />

</form:form>

<input name="radio" type="radio" id="Yes" value="yes" />Yes<br>
<input name="radio" type="radio" id="No" value="no"/>No<br>

What I did here was simply take the radio buttons out of the separate forms and grouped them together...pretty obvious; 我在这里所做的只是将单选按钮从单独的表单中取出,并将它们分组在一起……很明显; now let's look at the javascript file. 现在让我们看一下javascript文件。

    function goHere()
    {
var yesButton = $('#Yes');
var noButton = $('#No');
var str ="Please select an option first then press the 'Submit' button";
if (yesButton[0].checked) 
{
    submitForm('yesForm');
}
else if (noButton[0].checked)
{
    submitForm('noForm');
}
else 
{
    document.write(str.fontcolor.font("red"));
}
    }

As you can see the function 'goHere();' 如您所见,函数'goHere();' is going to tell the submit button in the following code where we want to go based on the user's selection on our radio buttons. 根据用户在单选按钮上的选择,将告诉以下代码中的提交按钮我们要去的地方。 Here's the call from our javascript function in a submit button on the form... 这是我们的javascript函数在表单上的“提交”按钮中进行的调用...

    <div class="button-panel" id="Submit"><span class="buttons buttons-left"></span>
<button type="button" class="buttons buttons-middle"  name="submitBtn"  onClick="goHere();">Submit</button>
<span class="buttons buttons-right"></span>


That's it!! 而已!! Simply put; 简单的说; sometimes, while it's invaluable to learn something new, if it's not broke--etc. 有时候,虽然学到一些新东西是很有价值的,但是如果它还没坏的话,等等。 Hope this helps someone later on down the line! 希望这对以后的人有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM