我正试图通过点击按钮启动一个功能。单击时,函数循环遍历每个input 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"/>
如果您需要更多信息,请告诉我们。 提前致谢
答案 0 :(得分:1)
由于它是ASP.NET,该ID将变得不同,尝试抓取客户端ID(如果JS在同一个文件中,如果不是,则使用一个唯一的类并通过它分配处理程序)< / p>
$("#<%=btnSave.ClientID%>").click(function() {
$("input:text").each(function() {
if (!this.value.length) {
var confirm = confirm('are you sure you want to save a blank row')
..
}
});
});
答案 1 :(得分:1)
对于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>
答案 2 :(得分:0)
你也可以这样做....
<script type="text/javascript">
function isInputEmpty() {
var bool = false;
$("#btnSave").click(function () {
$('input[type=text]').each(function () {
var x = $(this).attr('value');
if (x == null || x == "") {
bool = true;
}
});
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>
答案 3 :(得分:0)
目前还不完全清楚您的问题是什么,但此处的其他答案正确地集中在ID问题上,而.Net网络表格正在更改元素的ID。
建议的其他解决方案很好,但还有另一种方法,那就是通过部分ID进行搜索。当clientIDmode未设置为static(或者如果你是.Net 4.0之前)时,.NET ID将始终在下划线后附加原始id,因此您可以使用jquery找到您的元素,如下所示:
$("[id$='_btnSave']").click(function(){ ...