如何使用点击事件递增网址 ID 参数
How to increment url ID parameter with onclick event
在
JavaScript 中单击按钮时,是否可以递增 URL ID 并打开递增的页面?
例如,如果 ID 为 http://www.someurl.com/index.php?id=17
单击按钮以加载此 iframe 链接时:
http://www.someurl.com/index.php?id=18
<button onclick="increment()">Increment</button>
<div id="iframe">
<script>
var id = 17;
var link='<iframe src="http://www.myurl.com/index.php?id=' + id + ' " height="600px" width="100%" />';
function increment(){
id++;
document.getElementById('iframe').innerHTML = id + link;
}
</script>
</div>
OnClick Id 正在增加,但内容仍然相同...
HTML
<a href="http://www.someurl.com/index.php?id=10" id="nextPage">Next page</a>
<button onClick='increment()'>Increment</button>
现在将 url 分为 2 个变量,一个将存储 'id' 和另一个 id 的值
爪哇语
var url = document.getElementById('nextPage').href;
var urlPart1 = url.split('=')[0]; //get the url till id
var urlParameter = url.split('=')[1]; //get the id paramerter
var parameter = parseInt(urlParameter); // convert the id parameter into string
function increment() {
parameter += 1;
var newURL = urlPart1 + "=" + parameter;
document.getElementById('nextPage').href = newURL;
}
工作链接 : http://jsbin.com/pejasucuqu/edit?html,js,output
您可以使用 $_REQUEST 或其他方式获取页面的 ID。然后,您可以使用它打开url中id值递增的新窗口。
要了解如何打开新窗口,请通过此链接http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_open
假设您的 url 将始终是给定的格式(即 http://www.someurl.com/index.php?id=18),以下内容应该可以正常工作。
var start = 17;
$(".increment-btn").click(function() {
start++;
var next_url = "http://www.someurl.com/index.php?id=" + start;
$('#iframe').attr('src', next_url)
});
<button class="increment-btn" type="button">Increment Page</button>
<iframe name="iframe" id="iframe" src=""></iframe>
相关文章:
- 如何根据单击的按钮发送id参数
- 将参数中类似形式的ID传递给函数click()
- 如何传递元素's ID作为使用getElementById的函数的参数
- AngularJS-点击对象,访问它's ID&以该ID作为参数激发函数
- HTML span id 作为 JavaScript 函数参数
- 元素id未被接受为函数参数
- 通过函数参数传递id
- 当名称空间在id参数之前声明时,Angular UI路由器停止工作
- angular$resource delete-传递id参数
- Node.js 在使用 id 参数时在 dpd.get 上部署 404 错误,在 dpd.put 上部署 400 错误
- 如何在 ember.js 中访问路由文件中的 :id 参数
- 如何使用点击事件递增网址 ID 参数
- 从 javascript 中的 URL 检索 ID 参数
- 通过附加ID参数以编程方式设置变量名
- Razor@Url.Action返回一个javascript变量,并带有可选的id参数
- 如何从骨干路由器中提取id参数
- 传递ID参数到aspx模态窗口
- 将(ID)参数从aspx页面传递到javascript函数,该函数在页面的其他地方更新ID
- 通过ID参数参照另一个对象构建数组
- 将id参数从Datatable传递到控制器