1

我的问题是,如何确定用户是否按下“是”或“否”这是一个 html

<form id="delFrm" method="post">
    <label>Do you want delete?</label>
    <input type="submit" name="yes" id="yes" value="yes" />
    <input type="submit" name="no" id="no" value="no" />
</form>

这里是 jquery/js

<script type="text/javascript">
    $(document).ready(function() {
        $('#delFrm').submit(function(){
            var yes = true;//how to define it??????
            if(yes) {
                //sent ajax to delete the data
            } else {
                //close close the form on a fancybox
            }
            return false;
        });
    });
</script>

谢谢

4

3 回答 3

3

稍微改变你的标记:因为“否”按钮不需要执行 ajax 调用,你可以简单地写

<form id="delFrm" method="post">
    <label>Do you want delete?</label>
    <input type="submit" name="yes" id="yes" value="yes" />
    <button type="button" id="no">No</button>
</form>

并且您的 js 代码可以简化为

<script type="text/javascript">
    $(document).ready(function() {
        $('#delFrm').submit(function(evt){
            evt.preventDefault();
            //sent ajax to delete the data
        });
    });
</script>

然后为“否”按钮创建另一个处理程序(关闭您的幻想框)

于 2013-01-03T10:03:27.093 回答
0

您不需要更改您的 html 只需执行以下操作

<script type="text/javascript">
$(document).ready(function() {
    var answer = "";
    $('input[type=submit]').click(function(e) {
       e.preventDefault();
       answer = $(this).attr('name');
        if(asnwer == 'yes') {
            // sent ajax to delete the data
        } else {
            // close close the form on a fancybox
        }
        return false;
    });
});
</script>

就这么简单..

于 2013-01-03T10:06:37.257 回答
0

如果您必须调用执行某些语句来关闭 ajax 表单,请尝试此操作:

<form id="delFrm" method="post">
    <label>Do you want delete?</label>
    <input type="submit" name="yes" id="yes" value="yes" />
    <input type="button" name="no" id="no" value="no" />
</form>

<script type="text/javascript">
    $(document).ready(function() {
        $('#delFrm').submit(function(){

            //sent ajax to delete the data

            return false;
        });
        $('#no').click(function(){

            //close close the form on a fancybox

            return false;
        });
    });
</script>

希望能帮助到你 :)

于 2013-01-03T10:10:44.103 回答