我想生成目标文本框值取决于具有以下条件的另一个文本框值 -
假设第一个 TextBox 值 - “ myfile名称”
Copy of [1] myfile name
”。Copy of [1] myfile name
”,则目标值将为“Copy of [2] myfile name
”。Copy of [2] myfile name
”,则目标值将为“Copy of [3] myfile name
”。,即增加
如何使用jquery执行此操作?
HTML:
<div class="panel">
<input type="text" id="filename" value="filename">
</div>
<div class="panel">
<input type="text" id="newfilename" value="">
</div>
<div class="panel">
<button type="button" id="btnGenerate">
Get File Name
</button>
</div>
JS:
$('#btnGenerate').click(function(){
var newname;
var oldname = $('#filename').val();
if(typeof oldname !== undefined || oldname != ''){
if(oldname.indexOf('Copy of')){
var nCount = parseInt(oldname.match(/\([.*]\)/),10) + 1;
newname = "Copy of [" + nCount[1] + "] " + oldname;
$('#newfilename').val(newname);
}else{
newname = "Copy of [1] " + oldname'
}
$('#newfilename').val(newname);
}
});
答案 0 :(得分:0)
我对您的代码进行了以下调整:
if(oldname.substr(0, 7) == "Copy of"){
var nCount = parseInt(oldname.match(/\[(\d+?)\]/)[1],10) + 1;
var newname = oldname.replace(/\[\d+?\]/, "["+nCount+"]");
$('#newfilename').val(newname);
}
else
{
$('#newfilename').val("Copy of [1] " + oldname); //prepend Copy of [1]
}
看oldname.substr(0, 7) == "Copy of"
前indexOf
跳过if,而不执行代码。
现在[(\d+?)\]
真有效。它会取出[
和]
之间的所有数字,并将它们归入()
。 \d+?
表示获取1或n次出现的所有数字。 ?
将其lazy设为]
。
var newname = oldname.replace(/\[\d+?\]/, "["+nCount+"]")
只需对[]
块进行替换,然后将其替换为nCount
。
您甚至可以仅使用替换来优化它:
var newname = oldname.replace(/\[(\d+?)\]/, function(whole, group1){
return "[" + (parseInt(group1, 10)+1) + "]";
});
$('#btnGenerate').click(function(){
var oldname = $('#filename').val();
if(typeof oldname !== undefined || oldname != ''){
if(oldname.substr(0, 7) == "Copy of"){
var newname = oldname.replace(/\[(\d+?)\]/, function(whole, group1){
return "[" + (parseInt(group1, 10)+1) + "]";
});
$('#newfilename').val(newname);
}
else
{
$('#newfilename').val("Copy of [1] " + oldname); //prepend Copy of [1]
}
}
});
.panel{
display:block;
padding: 5px 10px;
}
.panel input{
width:100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="panel">
<input type="text" id="filename" value="Copy of [1] filename">
</div>
<div class="panel">
<input type="text" id="newfilename" value="">
</div>
<div class="panel">
<button type="button" id="btnGenerate">
Get File Name
</button>
</div>
在一个完全不同的主题上。您正在构建一个文件系统类型的控件。复制金额是由该文件名(和副本)的出现次数决定的不是更好吗?
答案 1 :(得分:0)
import java.util.Scanner;
public class SimpleCalculator
{
//-----------------------------------------------------------------
// Calculates two integers
// using values entered by the user.
//-----------------------------------------------------------------
public static void main(String[] args)
{
//Variables
final int ADDITION = 1,SUBTRACTION = 2, MULTIPLICATION = 3,DIVISION = 4, EXIT = 5;
int num1 = 0, num2 = 0, choice = 0;
double dblNum1, dblNum2, result;
String equation = "";
do
{
//Processing
equation = "";
Scanner scan = new Scanner(System.in);
System.out.println("Choose from the following: ");
System.out.println("1. Add 2 integers");
System.out.println("2. Subtract 2 integers");
System.out.println("3. Multiply 2 integers");
System.out.println("4. Divide 2 integers");
System.out.println("5. Exit");
System.out.print("Enter choice: ");
choice = scan.nextInt();
if(choice < 5 && choice > 0)//keeps program from asking for two numbers if exiting
{
System.out.print("Enter first integer: ");
num1 = scan.nextInt();
System.out.print("Enter second integer: ");
num2 = scan.nextInt();
}
//switch for operations
switch (choice)
{
case ADDITION:
result = num1 + num2;
equation = ((num1) + " + " + (num2) + " = "+ result);
break;
case SUBTRACTION:
result = num1 - num2;
equation = ((num1) + " - " + (num2) + " = "+ result);
break;
case MULTIPLICATION:
result = num1 * num2;
equation = ((num1) + " * " + (num2) + " = "+ result);
break;
case DIVISION:
if(num2 == 0)//when denominator becomes zero
{
System.out.println("DIVISION NOT POSSIBLE");
break;
}
dblNum1 = num1;//convert int to double
dblNum2 = num2;
result = dblNum1/dblNum2;
equation = ((num1) + "/" + (num2) + " = "+ result);
break;
case EXIT:
System.exit(0);
break;
default:
System.out.println("YOU HAVE ENTERED AN INCORRECT CHOICE");
}
//Output
System.out.println(equation);
System.out.println();
}while(choice != EXIT);
}
}
&#13;
names = new Array()
$('#btnGenerate').click(function(){
var oldname = $('#filename').val();
names.push(oldname);
if(oldname != ''){
var nCount = $.grep(names, function (elem) {
return elem === oldname;
}).length;
var newname = "Copy of [" +nCount + "] " + oldname;
$('#newfilename').val(newname);
}
});
&#13;
.panel{
display:block;
padding: 5px 10px;
}
.panel input{
width:100%;
}
&#13;
我希望这就是你要找的东西。
答案 2 :(得分:0)
<强> Updated fiddle 强>
你是如此接近,你只是稍微调整正则表达式并添加其他语句:
$('#btnGenerate').click(function(){
var oldname = $('#filename').val();
if(typeof oldname !== undefined || oldname != ''){
//Adding '>-1' in this condition
if(oldname.indexOf('Copy of')>-1)
{
var nCount = parseInt(oldname.match(/\[(.*)\]/)[1],10) + 1;
var file_name = oldname.split('] ')[1];
var newname = "Copy of [" + nCount + "] " + file_name;
}else{
var newname = 'Copy of [1] '+oldname;
}
$('#newfilename').val(newname);
}
});
希望这有帮助。
$('#btnGenerate').click(function(){
var oldname = $('#filename').val();
if(typeof oldname !== undefined || oldname != ''){
if(oldname.indexOf('Copy of')>-1){
var nCount = parseInt(oldname.match(/\[(.*)\]/)[1],10) + 1;
var file_name = oldname.split('] ')[1];
var newname = "Copy of [" + nCount + "] " + file_name;
}else{
var newname = 'Copy of [1] '+oldname;
}
$('#newfilename').val(newname);
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="panel">
<input type="text" id="filename" value="Copy of [1] filename">
</div>
<div class="panel">
<input type="text" id="newfilename" value="">
</div>
<div class="panel">
<button type="button" id="btnGenerate">
Get File Name
</button>
</div>
&#13;
答案 3 :(得分:0)
您的代码正常运行,但您必须从
更改正则表达式 /\([.*]\)/ to /\d+/