Javascript 单击按钮,循环浏览文本框
我试图通过点击按钮来实现一个功能。单击时,一个函数循环遍历每个输入类型=文本元素,将其值属性指定给变量,如果该变量=null或variable=“”调用方法确认(“是否确实要保存空行”)。 以下是我的代码/伪代码:Javascript 单击按钮,循环浏览文本框,javascript,jquery,asp.net,Javascript,Jquery,Asp.net,我试图通过点击按钮来实现一个功能。单击时,一个函数循环遍历每个输入类型=文本元素,将其值属性指定给变量,如果该变量=null或variable=“”调用方法确认(“是否确实要保存空行”)。 以下是我的代码/伪代码: <script type="text/javascript"> function isInputEmpty() { $("#btnSave").click(function(){ $('input[type=text]').
<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>
函数isInputEmpty(){
$(“#btnSave”)。单击(函数(){
$('input[type=text]')。每个(函数(){
var x=$(this.attr('value');
var bool=假;
如果(x==null | | x==“”)
{
布尔=真;
}
其他的
{
发送请求
}
if(bool==true)
{
如果(确认('您确定要保存空白URL吗?'))
{
发送请求
}
其他的
{
不执行任何操作或取消请求
}
}
其他的
{
发送请求
}
}
}
以下是我的asp按钮代码:
如果你需要更多的信息,请告诉我。
提前感谢由于它是ASP.NET,该ID将被呈现为不同的,请尝试获取客户端ID(如果JS在同一个文件中,如果不在,请使用唯一类并通过该类分配处理程序)
$(“#”)点击(函数(){
$(“输入:文本”)。每个(函数(){
如果(!this.value.length){
var confirm=confirm('您确定要保存一个空行')
..
}
});
});
您也可以像下面这样做
<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>
函数isInputEmpty(){
var bool=假;
$(“#btnSave”)。单击(函数(){
$('input[type=text]')。每个(函数(){
var x=$(this.attr('value');
如果(x==null | | x==“”){
布尔=真;
}
});
if(bool==true){
如果(确认('您确定要保存空白URL吗?')){
//发送请求
}
否则{
//不执行任何操作或取消请求
}
}
否则{
//发送请求
}
});
}
对于ID问题,如果使用ASP.Net 4.0+,请设置clientdmode=Static
<asp:Button ID="btnSave" runat="server" ClientIDMode="Static" Text="Save"/>
JS
函数isInputEmpty(){
$(“#btnSave”)。单击(函数(){
$('input[type=text]')。每个(函数(){
var x=该值;
var bool=假;
如果(x==null | | x===“”)
{
布尔=真;
}
其他的
{
发送请求
}
如果(bool==true)
{
如果(确认('您确定要保存空白URL吗?'))
{
LoadData();
}
其他的
{
return;//什么也不做
}
}
其他的
{
发送请求
}
}
}
函数LoadData()
{
$.ajax({
键入:“获取”,
url:'page.aspx',
超时:1000,
成功:函数(数据){
//工作
},
错误:函数(jqxhr,状态){
如果(状态=“错误”){
//处理错误
}
});
}
您的问题并不完全清楚,但这里的其他答案正确地集中在ID问题和.Net webforms更改元素ID上
建议的其他解决方案也不错,但还有另一种方法,即通过部分ID进行搜索。当clientIDmode未设置为静态(或者如果您是.Net 4.0之前的版本)时,.Net ID将始终在下划线后附加原始ID,因此您可以使用jquery查找元素,如下所示:
$("[id$='_btnSave']").click(function(){ ...
感谢您使用
this.value
$(this.val()
已经够糟糕的了,更不用说他使用的$(this.attr('value')
。@plantheidea--不必让jQuery高兴:)谢谢你清理我的代码。我的问题没有明确说明。我想知道我的javascript需要修改的地方,还有,我需要添加什么样的逻辑来代替//发送请求//不做任何评论。完整的代码是不必要的,尽管我很感激。这是一个非常有用的回答。我将把你的条目标记为answ呃,如果你也能帮我添加代码来代替伪代码“发送请求”和“不做任何事情或取消请求”@用户285535,根据您的请求添加了加载数据的LoadData
。请检查是否正确,我的问题没有明确说明。我希望对我的代码进行评论,概述明显的语法/逻辑错误。以及我需要添加什么逻辑来代替伪“发送请求/不执行任何操作”完整的代码示例是不必要的,尽管非常感谢。
<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>
$("[id$='_btnSave']").click(function(){ ...