jQuery 从模型中的输入中获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32275239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:52:31  来源:igfitidea点击:

Get value from input in model

jquerytwitter-bootstrapmodal-dialog

提问by Agis Soleas

I have a bootstrap model and inside I have an input field.

我有一个引导模型,里面有一个输入字段。

<input type="text" name="quota" class="form-control" id="quotas" value="'.$storagequota.'" maxlength="20" >

Also, I have a button which when clicked calls a function so I can get the value of the input field.

另外,我有一个按钮,单击该按钮时会调用一个函数,以便我可以获取输入字段的值。

function getQuota(){
    quota = $("#quotas").val();
    alert(quota);
}

The problem is that I cannot get the value that user typed. The weird thing is that i have another model in another page that works in the same way. Can someone help with that? Thanks in advance.

问题是我无法获得用户输入的值。奇怪的是,我在另一个页面中有另一个以相同方式工作的模型。有人可以帮忙吗?提前致谢。

回答by William Hammock

Just put the id of the modal before the id of the input

只需将模态的 id 放在输入的 id 之前

$("#registration_modal #useremail").val().trim()

回答by J Santosh

Your code is correct .

你的代码是正确的。

Just write the JS functionin headtag.

只需编写JS functioninhead标签。

Here is your code

这是你的代码

<html>    
<head>
    <script>
        function testFun() {
            quota = $("#quotas").val();
            alert(quota);
        }
    </script>
</head>

<body>
    <div id="admin" class="modal fade" role="dialog">
        <div class="modal-dialog">
            <!-- Modal content-->
            <div class="modal-content  animated bounceInDown">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">Settings</h4>

                </div>
                <div class="modal-body">
                    <ul class="nav nav-tabs">
                        <li class="active"><a href="#tab1" data-toggle="tab">Quota</a>

                        </li>
                    </ul>
                    <div class="tab-content">
                        <div class="tab-pane active" id="tab1">
                            </br>
                            <div id="table" class="col-lg-12"></div>
                            <label for="Quota">Quota</label>
                            <div class="input-group form-group">
                                <input type="text" name="quota" class="form-control" id="quotas" value="10" maxlength="20">
                                <div class="input-group-addon input-group-btn-custom "> <span id="type">Gb</span> 
                                </div>
                            </div>
                        </div>
                        </br>
                        </br>
                        <button type="submit" class="btn btn-primary" onclick="testFun()" id="admin">Submit</button>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
</body>

</html>

Working Demo

工作演示