jQuery输入复杂的增量混乱 [英] Jquery input complicated incrementing mess

查看:76
本文介绍了jQuery输入复杂的增量混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含文本的输入,当您单击它时,该文本消失了,但是如果您未输入任何内容,文本将返回.

I have this input that has text and when you click it, the text dissapears but if you didnt type anything the text will come back.

<input type="button" id="btnAdd" value="Add Answer" />
<input type="text" id="forminput" class="clonedInput" onclick="this.value='';" 
onfocus="this.select()"onblur="this.value=!this.value?'Answer 1
':this.value;" value="Answer 1" />

我有一个按钮,可以使用此功能克隆并增加我当前的输入字段.

I have a button that clones and increments my current input field using this function.

这是增加我当前输入字段的函数,但我还需要增加Answer#.

This is the function incrementing my current input field but i also need to increment the Answer #.

$('#btnAdd').click(function() {
var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
var newNum = new Number(num + 1); // the numeric ID of the new input field being added

// create the new element via clone(), and manipulate it's ID using newNum value
var newElem = $('#formanswer' + num).clone().attr('id', 'formanswer' + newNum);

// manipulate the name/id values of the input inside the new element
newElem.children(':first').attr('id', 'formanswer' + newNum).attr('value','Answer '+newNum);

 // insert the new element after the last "duplicatable" input field
 $('#formanswer' + num).after(newElem);

我的问题是我需要在新输入字段内增加文本,以使其每次克隆时都动态增加一.香港专业教育学院一直禁止我的头靠墙几个小时,但我希望这不是一些简单的PHP VAR.请帮我:〜)

My problem is that i need to increment the text inside the new input field to make it dynamically incrementing by one each time it is cloned. Ive been baning the wall against my head for hours but i hope it isnt some easy php var. plz help me :~)

推荐答案

这是一个正在工作的小提琴,我认为您正在尝试做的事情:

Here's a fiddle which is working, and I think does what you're attempting:

http://jsfiddle.net/R2tFz/

为了后代,这是我创建的javascript:

For posterity, here is the javascript I created:

$(function () {

$('#btnAdd').click(function() {
var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
//clone the last field    

var newNum = num + 1; // the numeric ID of the new input field being added

    var newElem = $('#formanswer' + num).clone().attr('id', 'formanswer' + newNum);

// create the new element via clone(), and manipulate it's ID using newNum value

// manipulate the name/id values of the input inside the new element
$("input", newElem).attr('name', 'formanswer' + newNum).val('Answer ' + newNum);

 // insert the new element after the last "duplicatable" input field
 newElem.insertAfter($('#formanswer' + num));

});


    $(".clonedInput").live('blur', function () {
     var num = $(this).attr('name').replace('formanswer', '');
     $(this).val( (!$(this).val().length)?('Answer '+num):$(this).val() );
    }); 
});

这篇关于jQuery输入复杂的增量混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆