最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery change button attributes - Stack Overflow

programmeradmin8浏览0评论

I have a button like this:

<input type="button" name="btn" id="btn" value="Proceed"/>

After all processes and get the expected result, I need to apply some changes on this button.

  1. Change the value of this button.
  2. Add something to direct the current page to another one.

I know I can do the first goal using the code below:

$('#btn').attr('value','newValue');

But for the second one, I need something like our previous codes in JavaScript as below:

onclick="window.location.href='newPage.htm'";

Help me please.

I have a button like this:

<input type="button" name="btn" id="btn" value="Proceed"/>

After all processes and get the expected result, I need to apply some changes on this button.

  1. Change the value of this button.
  2. Add something to direct the current page to another one.

I know I can do the first goal using the code below:

$('#btn').attr('value','newValue');

But for the second one, I need something like our previous codes in JavaScript as below:

onclick="window.location.href='newPage.htm'";

Help me please.

Share Improve this question asked Nov 28, 2011 at 12:24 Mohammad SaberiMohammad Saberi 13.2k28 gold badges81 silver badges131 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 1

something like, if I understand you correctly:

$("#btn").val("newValue").click(function(){
     document.location = "newPage.htm";
});

This will set a new value and bind a click handler.

Try this:

$("#btn").attr("onclick", "window.location.href='newPage.htm'");

Or better yet, add a click handler:

$("#btn").click(function() {
    window.location.assign = 'newPage.htm';
});

You can do something like this :

$("#btn").val("newValue").click(function(){
document.location = "newPage.htm"; 
});

Try this:

<script type="text/javascript">
$(document).ready(function() {
    $("#btn").click(function() {
        $('#btn').attr('value', 'newValue');
        window.location="newPage.htm";
    });
});
</script>
发布评论

评论列表(0)

  1. 暂无评论