我对如何从HTML <select>
项目中获取所选选项的索引感到有点困惑.
在此页面上有两种描述的方法.但是,两者都在回归-1
.这是我的jQuery代码:
$(document).ready(function(){
$("#dropDownMenuKategorie").change(function(){
alert($("#dropDownMenuKategorie option:selected").index());
alert($("select[name='dropDownMenuKategorie'] option:selected").index());
});
});
Run Code Online (Sandbox Code Playgroud)
并在HTML中
(...)
<select id="dropDownMenuKategorie">
<option value="gastronomie">Gastronomie</option>
<option value="finanzen">Finanzen</option>
<option value="lebensmittel">Lebensmittel</option>
<option value="gewerbe">Gewerbe</option>
<option value="shopping">Shopping</option>
<option value="bildung">Bildung</option>
</select>
(...)
Run Code Online (Sandbox Code Playgroud)
为什么会这样?select
在分配change()
方法时,是否有可能没有"准备好" ?此外,换.index()
到.val()
的返回正确的值,所以这是混淆了我,甚至更多.
我正在编写一个自定义的"矢量"结构.我不明白为什么我要Warning: "one" may be used uninitialized
来这里.
这是我的vector.h文件
#ifndef VECTOR_H
#define VECTOR_H
typedef struct Vector{
int a;
int b;
int c;
}Vector;
#endif /* VECTOR_ */
Run Code Online (Sandbox Code Playgroud)
警告在线发生 one->a = 12
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include "vector.h"
int main(void){
Vector* one;
one->a = 12;
one->b = 13;
one->c = -11;
}
Run Code Online (Sandbox Code Playgroud) 我从包含德语变音符号的数据库中获取数据时遇到了麻烦.基本上,每当我收到包含变音符号的数据时,它都是带有询问标记的黑色方块.我把它解决了
mysql_query ('SET NAMES utf8')
Run Code Online (Sandbox Code Playgroud)
在查询之前.
问题是,一旦我json_encode(...)
在查询结果上使用,包含变音符号的值就会得到null
.我可以通过直接在浏览器中调用php文件来看到这一点.除了在编码到JSON之前替换这些字符并在JS中解码它之外还有其他解决方案吗?
在我的Swing应用程序中,用户必须插入数字和值,然后才能切换到下一个窗口.现在作为一个干净的程序,我检查每个输入是否有效,如果没有,则显示错误消息,下一个窗口不打开.
该检查的结构如下(示例):
Button buttonToOpenNextWindow = new JButton("next");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(checkValidty){
// (...)
new WindowA();
frame.dispose(); // (*)
}
}
});
Run Code Online (Sandbox Code Playgroud)
(*)注意:我知道多个JFrame的原理是丑陋的,我正在改变它,但对于这个问题它是无关紧要的.
现在这个问题的重点是这个checkValidity()
,我的结构是这样的:
private boolean checkValidity(){
// check input 1
try{
Integer.parseInt(textField1.getText());
}catch (NumberFormatException e){
new ErrorDialog("input 1 is invalid!"); // own implemented dialog
return false;
}
// check input 2
try{
Integer.parseInt(textField2.getText());
}catch (NumberFormatException e){
new ErrorDialog("input 2 is invalid!"); // own implemented dialog
return false;
}
// (...)
// check input n …
Run Code Online (Sandbox Code Playgroud) 有什么办法找出多少像素宽一定String
在某Font
是什么?
在我的Activity
,有动态String
s穿上了Button
.有时,String
太长了,它分为两行,这Button
看起来很难看.但是,由于我不使用某种控制台Font
,因此单个字符宽度可能会有所不同.所以写这样的东西并不是一种帮助
String test = "someString";
if(someString.length()>/*someValue*/){
// decrement Font size
}
Run Code Online (Sandbox Code Playgroud)
因为"mmmmmmmm"比"iiiiiiii"宽.
或者,Android中是否有一种方法可以String
在一条线上安装一定的线,因此系统会Font
自动"缩放" 尺寸?
编辑:
既然wsanville的答案真的很好,这是我的代码动态设置字体大小:
private void setupButton(){
Button button = new Button();
button.setText(getButtonText()); // getButtonText() is a custom method which returns me a certain String
Paint paint = button.getPaint();
float t = 0;
if(paint.measureText(button.getText().toString())>323.0){ //323.0 is the max width fitting in the button
t = getAppropriateTextSize(button);
button.setTextSize(t); …
Run Code Online (Sandbox Code Playgroud) 我想知道是否有可能以这种方式实现JFrame的大小调整,它已被调整大小,例如linux中的标准窗口.更确切地说:如果用户开始拖动,则只有未来的大小才能预览窗口,而原始内容不会调整大小.一旦用户释放鼠标,Frame就会调整为该大小.在图像中:
(1)调整大小之前的状态
(2)用户开始下垂(在红色圆圈)
(3)用户释放鼠标,框架调整大小
是否有可能在Java Swing中意识到这一点?
编辑:
由于这个程序有一天也应该在较低的Java RE中作为7运行,我尝试将mKorbel建议与translucend Frame的注释中的建议结合起来.结果接近目标,除了
我认为第一点可以通过代码和MouseListener的组合来解析,类似于mouseReleased(),然后调整大小.这是代码,随意尝试.对于进一步的建议,我仍然对任何建议感到高兴.
该代码稍微修改了Java Tutorials 中的GradientTranslucentWindowDemo.java.我希望它可以在这里发布,否则请告诉我任何侵犯版权的行为.黑色JPanel应该是应用程序的内容,因为contentPane保持不可见.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;
public class GroundFrame extends JFrame {
Timer timer;
JPanel panel2;
public GroundFrame() {
super("GradientTranslucentWindow");
setBackground(new Color(0,0,0,0));
setSize(new Dimension(300,200));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
panel.setBackground(new Color(0,0,0,0));
setContentPane(panel);
setLayout(null);
panel2 = new JPanel();
panel2.setBackground(Color.black);
panel2.setBounds(0,0,getContentPane().getWidth(), getContentPane().getHeight());
getContentPane().add(panel2);
addComponentListener(new ComponentListener() {
@Override
public void componentShown(ComponentEvent e) …
Run Code Online (Sandbox Code Playgroud) 看看这个简单的小提琴option
里面浮动的select
.
这似乎在Chrome和Edge中完美运行,但在Firefox中则不然.为了在Firefox中获得相同的结果,有没有可能或黑客?
<select size="8">
<option></option>
<option></option>
<option></option>
<option></option>
<option class="flip"></option>
<option class="flip"></option>
<option class="flip"></option>
<option class="flip"></option>
</select>
select{
width: 420px;
height: 200px;
overflow: hidden;
}
option{
width: 100px;
height: 100px;
float: left;
}
select option:nth-child(odd){
background: #aaa;
}
select .flip:nth-child(odd){
background: #fff;
}
select .flip:nth-child(even){
background: #aaa;
}
Run Code Online (Sandbox Code Playgroud) 我试图从数据库中获取一些信息,然后使用该信息来获取一些统计信息.
我想根据一个小时的间隔获得统计数据,因此我创建了一个HashSet
由两个组成的统计数据Integer
小时和数据组成的数据.
为了获得正确的小时,我需要从数据库中获取时间.因此,我需要创建某种数据/日历对象.
现在Date
已经弃用了,我需要找到一种新的方法来设置小时数.
有谁知道我怎么能做到这一点?
到目前为止,这个解决方
Calendar time = Calendar.getInstance();
time.setTime(new Date(2012, 11, 12, 8, 10));
int hour = time.get(Calendar.HOUR);
System.out.println(hour);
Run Code Online (Sandbox Code Playgroud)
但如上所述,日期已被弃用,所以我想学习"正确"的方法.
我想使用Levenshtein算法执行以下任务:如果我网站上的用户搜索某些值(他在输入中输入字符),我想立即检查AJAX的建议,就像Google Instant一样.
我的印象是Levenshtein算法对于这样的任务来说太慢了.为了检查它的行为,我首先用Java实现它String
,在方法的每次递归调用中打印出两个s.
public class Levenshtein {
public static void main(String[] arg){
String a = "Hallo Zusammen";
String b = "jfdss Zusammen";
int res = levenshtein(a, b);
System.out.println(res);
}
public static int levenshtein(String s, String t){
int len_s = s.length();
int len_t = t.length();
int cost = 0;
System.out.println("s: " + s + ", t: " + t);
if(len_s>0 && len_t>0){
if(s.charAt(0) != t.charAt(0)) cost = 1;
}
if(len_s == 0){
return len_t;
}else{
if(len_t == …
Run Code Online (Sandbox Code Playgroud) 为了在我的脑中提供自定义字体,我在这里ListActivity
写了一个根据这个例子CustomAdapter
扩展的类.BaseAdapter
但是,正如那里所描述的那样,我编写了getView()
如下方法:
public View getView(int position, View convertView, ViewGroup parent){
String gameName = gameNames[position]; // gameName ist the String[] of the Custom Adapter
TextView tv = new TextView(context);
tv.setText(gameName);
tv.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/gulim.ttf"));
return tv;
}
Run Code Online (Sandbox Code Playgroud)
这按预期工作.唯一令人不安的是,在列表显示之前需要大约三到四秒钟(在这种情况下这是一个很长的时间).但是,在ListActivity
我设置onItemClickListener
s像这样:
private void setOnItemClickListener(){
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int pos, long id){
onClickEntryButton(((TextView) view).getText().toString());
}
});
}
private void onClickEntryButton(String gameName){
Intent intent = new Intent(this, GameActivity.class);
intent.putExtra("gameName", gameName); …
Run Code Online (Sandbox Code Playgroud)