简体   繁体   English

HTML表单单选按钮以使用jQuery单击提交或隐藏

[英]HTML form radio button to submit or hide on click with jquery

I am trying to use jquery to either submit or hide depending on the radio button that is clicked. 我正在尝试使用jquery来提交或隐藏,具体取决于单击的单选按钮。 I am submitting the value to mysql. 我正在将值提交给mysql。 The radio buttons are in a tr. 单选按钮位于tr中。 If I include a submit button in the form I can update mysql with the value successfully. 如果我以表单的形式包含一个提交按钮,则可以成功地用该值更新mysql。

<tr>
    <form name="MyStatusForm" id="MyStatusForm" method="post" action="update.php">
        <td class="statuscolumn">
            <input name='status' type='radio' value='1' />
            <label for="new">New</label>
            <input name='status' type='radio' value='2' />
            <label for="accept">Accept</label>
            <input name='status' type='radio' value='3' />
            <label for="resolve">Resolve</label>
        </td>
    </form>
</tr>

My script looks for the value of the radio button to determine which action to take. 我的脚本查找单选按钮的值,以确定要采取的操作。

$('input[type=radio]').click(function () {
    var $closestTR = $(this).closest('tr');
    var $closestForm = $(this).closest('form');
    if (this.value == '1') || (this.value == '2') {
        $closestForm.submit();
    }
    if (this.value == '3') {
        var ok = confirm("some message?");
        if (ok == true) {
            $closestTR.hide("slow");
        } else {
            return;
        }
    }
});

The hide works fine. 皮革很好用。 It is only the submit that doesn't work. 只是提交无效。 There is no response when I click the radio button and mysql is not updated. 单击单选按钮并且没有更新mysql时没有响应。

Thanks for your help 谢谢你的帮助

Working Demo 工作演示

Corrected Markup 更正的标记

HTML 的HTML

<table>
    <tr>
        <td class="statuscolumn">
            <form>
                <input name='status' type='radio' value='1' />
                <label for="new">New</label>
                <input name='status' type='radio' value='2' />
                <label for="accept">Accept</label>
                <input name='status' type='radio' value='3' />
                <label for="resolve">Resolve</label>
            </form>
        </td>
    </tr>
</table>

js js

$('input[type="radio"][name="status"]').click(function () {
    var $closestTR = $(this).parents('tr');
    $closestForm = $(this).parents('form');
    if (($(this).attr('value') == '1') || ($(this).attr('value') == '2')) {
        $closestForm.submit();
    }
    if (this.value == '3') {
        var ok = confirm("some message?");
        console.log(ok);
        if (ok == true) {
            $closestTR.hide();
        } else {
            return;
        }
    }
});

You have a syntax error in your if, should be 您的if中有语法错误,应为

if ((this.value == '1') || (this.value == '2'))

Also, don't forget to fix your markup as @adeneo suggested 另外,不要忘记按照@adeneo的建议修复标记

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

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