我正在尝试创建一个通过单击按钮启动的功能。单击时,函数循环遍历每个输入 type=text 元素,将其 value 属性分配给变量,如果该变量 = null 或变量 = ""调用方法确认('是否确定要保存空白行')。 这是我的代码/伪代码:
<script type="text/javascript">
function isInputEmpty() {
$("#btnSave").click(function(){
$('input[type=text]').each(function(){
var x = $(this).attr('value');
var bool = false;
if(x == null || x == "")
{
bool = true;
}
else
{
send the request
}
if(bool == true)
{
if(confirm('Are you sure you want to save a blank URL?'))
{
send the request
}
else
{
do nothing or cancel the request
}
}
else
{
send the request
}
}
}
</script>
这是我的 asp 按钮代码:
<asp:Button ID="btnSave" runat="server" Text="Save"/>
如果您需要更多信息,请告诉我。 提前致谢
最佳答案
对于ID问题,如果您使用ASP.Net 4.0+,请设置ClientIDMode=Static
<asp:Button ID="btnSave" runat="server" ClientIDMode="Static" Text="Save"/>
JS
<script type="text/javascript">
function isInputEmpty() {
$("#btnSave").click(function(){
$('input[type=text]').each(function(){
var x = this.value;
var bool = false;
if(x === null || x === "")
{
bool = true;
}
else
{
send the request
}
if(bool === true)
{
if(confirm('Are you sure you want to save a blank URL?'))
{
LoadData();
}
else
{
return;//do nothing
}
}
else
{
send the request
}
}
}
function LoadData()
{
$.ajax({
type: "GET",
url: 'page.aspx',
timeout: 1000,
success:function(data){
//do work
},
error:function(jqxhr, status){
if(status==="error"){
//handle error
}
});
}
</script>
关于javascript - 单击按钮时,循环遍历文本框 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19863043/