使用React获取文本而不是下拉(选择)输入的值

时间:2015-12-03 21:01:30

标签: javascript html5 validation dom reactjs

使用反应我可以通过func loadingScreen() { //Initialize loading sprite. let loadBg = SKSpriteNode(imageNamed: "loadBg.png"); loadBg.size = CGSize(width: self.size.width, height: self.size.height); loadBg.position = CGPoint(x: self.size.width/2, y: self.size.height/2); loadBg.zPosition = 10; self.addChild(loadBg); let loadBar = SKSpriteNode(imageNamed: "load1.png"); loadBar.size = CGSize(width: self.size.width/3, height: (self.size.width/3)*0.20); loadBar.position = CGPoint(x: self.size.width/2, y: self.size.height*0.1); loadBar.zPosition = 11; self.addChild(loadBar); var loadAni:[SKTexture] = []; for index in 1...17 { loadAni.append(SKTexture(imageNamed: "load\(index).png")); } loadBar.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(loadAni, timePerFrame: 0.01))); let qualityOfServiceClass = QOS_CLASS_BACKGROUND let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0) dispatch_async(backgroundQueue, { // Codes. self.startGame() dispatch_async(dispatch_get_main_queue(), { () -> Void in // Codes after loading. self.objectSlider.runAction(SKAction.repeatActionForever(SKAction.moveByX(-self.size.width/5.8, y: 0, duration: 0.5))) self.printPlayer() self.isGameOver = false; loadBg.runAction(SKAction.sequence([SKAction.fadeOutWithDuration(0.2),SKAction.removeFromParent()])) loadBar.runAction(SKAction.sequence([SKAction.fadeOutWithDuration(0.2),SKAction.removeFromParent()])) }) }) }

获取所选元素的值

但是在我目前的情况下,我希望将值作为其排序ID和选项的面值。我怎么抓住反应呢?这看起来有道理吗?

按面值,我指的是target.value

之间的单词

我希望能够获得“TEXT”

2 个答案:

答案 0 :(得分:1)

我只是给这个选择框一个id,并使用附加到你的点击处理程序的普通JavaScript函数或你想在你的React组件中执行的任何函数来获取这个值。

//常规javascript方式

var yourElement = document.getElementById(elementId);

    if (yourElement.selectedIndex == -1)
        return null;

    return yourElement.options[yourElement.selectedIndex].text;

答案 1 :(得分:0)

event.target为您提供HTMLSelectElement,您可以访问其他一些内容,例如HTMLSelectElement.selectedOptions

From MDN

  

HTMLSelectElement.selectedOptions(只读)

     

返回包含所选选项集的实时HTMLCollection。

从那里你可以得到HTMLOptionElementtext

把它们放在一起,你就会有这样的东西:

var selectedOption = e.target.selectedOptions[0];
console.log(selectedOption.value); // 123
console.log(selectedOption.text); // TEXT