简体   繁体   English

在回发ASP.NET之前使用javascript(Jquery)更改文本框的值

[英]Change value of text box using javascript (Jquery) before post back ASP.NET

I am trying to change the value of a set of text boxes (that hold greyed out suggestions in them) when the user presses the submit form button. 当用户按下提交表单按钮时,我试图更改一组文本框的值(其中包含灰色的建议)。 This button runs a server side method using OnClientClick that submits the data and does a whole slew of other things. 此按钮使用OnClientClick运行服务器端方法,该方法提交数据并执行大量其他操作。

Now my problem is that i can't either: fit in a javascript function that will change the values before the server gets hit, OR call the server side method in the javascript instead of the button OnClientClick event. 现在我的问题是我不能:适合在服务器被命中之前更改值的javascript函数,或者在javascript中调用服务器端方法而不是按钮OnClientClick事件。

Ive tried: 我试过了:

$(this.form).submit(function(){
//Change value here
});
//using OnClientClick to call function

and

$("#"+"<%=submitBtn.ClientID %>").click(function(){
    //Change values here
    __doPostBack("<%=submitBtn.ClientID %>");
});
//not using OnClientClick to call server method

(pretty sure that won't work) and (相当肯定不会起作用)和

$("#"+"<%=submitBtn.ClientID %>").click(function(){
    //Change values here
});
//using OnClientClick still to call function

Im stumped 我很难过


Edit 编辑

Right I obviously didn't give enough info, What happens when I use submit events is the server event fires before the JavaScript event, therefore when do a server side validation before I send the values away, I have the wrong values, there isn't any point in changing to client side validation because i will still have the same problem when I send the form data back to the db. 对,我显然没有提供足够的信息,当我使用提交事件时会发生什么事情是服务器事件在JavaScript事件之前触发,因此在我发送值之前进行服务器端验证时,我的值不正确,没有' t更改为客户端验证的任何一点,因为当我将表单数据发送回db时,我仍然会遇到同样的问题。


Update 更新

So i still have a problem (both with this and mentally because of this). 所以我仍然有一个问题(因为这个,这个和精神上都有)。
Because of the idiots who worked on this before me (now i have to fix it) they removed the submit behaviour (asp.net) from the button at the bottom, because they use some server side trickery to figure out if some validators should be on or off (when really it should be client side that does that), hence they had to turn it off because it would fire validators if it didn't. 因为在我之前为此工作的白痴(现在我必须解决它)他们从底部的按钮中删除了提交行为(asp.net),因为他们使用一些服务器端技巧来弄清楚是否应该有一些验证器打开或关闭(当它真的应该是客户端那样做),因此他们不得不关闭它,因为它会激活验证器,如果它没有。

ANYWAY... So I'm still having trouble, the on click function for the button doesn't seem to fire in time or the scripts run simultaneously. 无论如何...所以我仍然遇到麻烦,按钮的点击功能似乎没有及时触发或脚本同时运行。 I tested this by adding an alert and a breakpoint on the code behind, the breakpoint fires and the alert fires too. 我通过在后面的代码上添加警报和断点来测试这一点,断点触发并且警报也会触发。 sooo..... yeah. sooo .....是的。 Is there any way i could maybe circumvent this by removing the "onclientclick" from the button and calling the function it calls in the CB? 有没有办法可以通过从按钮中删除“onclientclick”并调用它在CB中调用的函数来绕过这个? Any ideas? 有任何想法吗? (Please?) (请?)


Small update: 小更新:

Still can't figure it out :(. Is anyone confused by the question? 仍然无法弄明白:(。 有人对这个问题感到困惑吗?

Yup, this function should work 是的,这个功能应该有效

$(this.form).submit(function(){
//Change value here
});

But one culprit might be the commented part: "// Change values here." 但是一个罪魁祸首可能是评论部分:“//在这里改变价值观。” If you're using one of these, 如果你正在使用其中一个,

$('#target').text('my new info');
$('#target').html('my new info');

...you will have trouble. ......你会有麻烦的。 You need to use .val() 你需要使用.val()

$('#target').val('my new info');

Just stumbled across this. 偶然发现了这个。 It's been a month since you first posted the question so you may have fixed it already. 自您第一次发布问题以来已经过了一个月,所以您可能已经修复了这个问题。 However, thought I'd add my thoughts anyway. 但是,我想我还是会添加自己的想法。

First, it's make sense your code is being ignored. 首先,你的代码被忽略是有道理的。 When you bind to the submit and onclick events you functions are added to the list of events handles. 绑定到submit和onclick事件时,您的函数将添加到事件句柄列表中。 Events handlers are processed from the first added to the last added. 事件处理程序从第一个添加到最后添加处理。 So the postback is started before your jquery code is called. 因此,在调用jquery代码之前启动回发。

To solve this you need to change the code in the function that OnClientClick calls or write a wrapper function that calls your code then calls what OnClientClick called and change OnClientClick to call your wrapper function. 要解决此问题,您需要更改OnClientClick调用的函数中的代码,或者编写一个调用代码的包装函数,然后调用OnClientClick调用的函数并更改OnClientClick以调用您的包装函数。

The form is submitted before JS fires all events. JS触发所有事件之前提交表单。

Have your button's onClientClick event change the values. 让按钮的onClientClick事件更改值。 You can call functions in sequence if you need to. 如果需要,可以按顺序调用函数。

jQuery does some funky things with events and you can't be sure what order they will fire after they are attached. jQuery在事件中做了一些时髦的事情,你不能确定它们附加后会触发什么样的命令。 You must explicitly specify the functions to call. 您必须明确指定要调用的函数。

<button id="some_button" onclick="SetValues(); SubmitForm();" />

this function should do the trick: 这个功能应该做的诀窍:

$(this.form).submit(function(){
//Change value here
});

just make sure you enable the text fields again before you send them to the server, otherwise the server won't pick them up 只需确保在将文本字段发送到服务器之前再次启用它们,否则服务器将不会启动它们

I have used id of submit button to check for a "click" 我使用提交按钮的ID检查“点击”

http://jsfiddle.net/sreeprasad/7urvg/ http://jsfiddle.net/sreeprasad/7urvg/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM